package com.steven.im.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.steven.im.R;
import com.steven.im.base.BaseActivity;
import com.steven.im.entity.BaseEntity;
import com.steven.im.net.MyCallback;
import com.steven.im.net.Task;
import com.steven.im.utils.TitleBuilder;
import com.steven.im.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

/**
 * 修改昵称
 */
public class EditnameActivity extends BaseActivity {

    @BindView(R.id.et_nickname)
    EditText etNickname;

    @Override
    public void setListen() {

    }

    @Override
    public void setTitleBarColor() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_editname;
    }

    @Override
    public void initView() {
        new TitleBuilder(this).setTitleText("设置")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public void initData() {
        String nickname = getIntent().getStringExtra("nickname");
        etNickname.setText(nickname);
        etNickname.setSelection(nickname.length());
    }


    @OnClick(R.id.tv_save)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_save://保存
                if (TextUtils.isEmpty(etNickname.getText().toString().trim())) {
                    ToastUtil.showShort(this, "请输入昵称");
                    return;
                }
                editUserInfo();
                break;
        }
    }

    /**
     * 修改昵称
     */
    private void editUserInfo() {
        String nickname = etNickname.getText().toString().trim();
        Task.getApiService().editMyInfo(nickname, "").enqueue(new MyCallback<BaseEntity>(this) {
            @Override
            protected void onSuccess(Response<BaseEntity> response) {
                try {
                    ToastUtil.showShort(EditnameActivity.this, response.body().getMessage());
                    if ("0".equals(response.body().getCode())) {
                        Intent it = new Intent();
                        it.putExtra("nickname", etNickname.getText().toString().trim());
                        setResult(RESULT_OK, it);
                        finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(Throwable t) {
            }
        });
    }
}
